﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;

using Microsoft.Xna.Framework.Graphics;
using imaginecup.util;
using Microsoft.Xna.Framework;

namespace Imaginecup.util.resourceMan
{
    public class ResourceMan
    {
        private static ResourceMan m_ResourceMan = null;
        private ResourceMan()
        {
            m_TextureMan = new Dictionary<string, Texture2D>();
            m_FrameSizeMan = new Dictionary<string, Vector2>();
        }

        private Dictionary<String, Texture2D> m_TextureMan;
        private Dictionary<String, Vector2> m_FrameSizeMan;

        public static ResourceMan GetResourceMan()
        {
            if (m_ResourceMan == null)
            {
                m_ResourceMan = new ResourceMan();


            }
            return m_ResourceMan;
        }

        public void Release()
        {
            foreach (KeyValuePair<String, Texture2D> texture_Pair in m_TextureMan)
            {
                texture_Pair.Value.Dispose();
            }
            m_TextureMan.Clear();
            m_FrameSizeMan.Clear();
        }

        public void Release(String KeyName)
        {
            if (!m_TextureMan.ContainsKey(KeyName)) return;

            Texture2D texture = m_TextureMan[KeyName];
            texture.Dispose();

            m_TextureMan.Remove(KeyName);
            m_FrameSizeMan.Remove(KeyName);
        }

        public Texture2D GetTexture(String key)
        {
            if (m_TextureMan.ContainsKey(key))
                return m_TextureMan[key];
            return null;
        }

        public Vector2 GetFrameSize(String key)
        {
            return m_FrameSizeMan[key];
        }

        public void SetTextureFrame(String key, Texture2D texture2D)
        {
            if (m_TextureMan.ContainsKey(key))
                return;

            m_TextureMan.Add(key, texture2D);
            m_FrameSizeMan.Add(key, GetSourceFullSize( key ));
        }

        public void SetTextureFrame(String key, Texture2D texture2D , Vector2 size)
        {
            if (m_TextureMan.ContainsKey(key))
                return;

            m_TextureMan.Add(key, texture2D);
            m_FrameSizeMan.Add(key, size);
        }

        private Vector2 GetSourceFullSize( String key )
        {
            Texture2D texture = m_TextureMan[key];
            return new Vector2(texture.Width, texture.Height);
        }

    }
}
